package com.wn.class30.service.impl;

import com.wn.class30.entity.Franchisees;
import com.wn.class30.entity.FranchiseesExample;
import com.wn.class30.mapper.FranchiseesDao;
import com.wn.class30.service.FranchiseesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
public class FranchiseesServiceImpl implements FranchiseesService {
    @Resource
    private FranchiseesDao franchiseesDao;
    @Override
    public int selectCount(HashMap map) {
        FranchiseesExample ex = getExample(map);
        return (int) franchiseesDao.countByExample(ex);
    }

    @Override
    public Object select(HashMap map) {
        FranchiseesExample ex = getExample(map);
        return franchiseesDao.selectByExample(ex);
    }

    @Override
    public void deleteById(Integer franchiseesId) {
        franchiseesDao.deleteByPrimaryKey(franchiseesId);
    }

    @Override
    public void updateFranchisees(Franchisees franchisees) {
        franchiseesDao.updateByPrimaryKeySelective(franchisees);
    }

    @Override
    public void insertFranchisees(Franchisees franchisees) {
        franchiseesDao.insertSelective(franchisees);
    }
    public FranchiseesExample getExample(HashMap map) {
        FranchiseesExample ex = new FranchiseesExample();

        String name = (String) map.get("name");
        String tel = (String) map.get("tel");
        String city = (String) map.get("city");
        String status = (String) map.get("status");
        Integer page = 0;
        if (!"".equals((String) map.get("page")) && (String) map.get("page") != null) {
            page = Integer.parseInt((String) map.get("page"));
        }
        Integer limit = 0;
        if (!"".equals((String) map.get("limit")) && (String) map.get("limit") != null) {
            limit = Integer.parseInt((String) map.get("limit"));
        }
        ex.setOffset(((long) (page - 1)) * limit);
        ex.setLimit(limit);
        FranchiseesExample.Criteria criteria = ex.createCriteria();
        if (!"".equals(name) && name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!"".equals(status) && status != null) {
            criteria.andStatusEqualTo(Integer.parseInt(status));
        }
        if (!"".equals(tel) && tel != null) {
            criteria.andTelEqualTo(tel);
        }
        if (!"".equals(city) && city != null) {
            criteria.andCityLike("%" + city + "%");
        }
        return ex;
    }
}
